package com.fr.data.impl;

import com.fr.base.FRContext;
import com.fr.base.StringUtils;
import com.fr.base.core.ComparatorUtils;
import com.fr.base.core.json.JSONObject;
import com.fr.base.file.DatasourceManager;
import com.fr.base.xml.XMLPrintWriter;
import com.fr.base.xml.XMLableReader;
import com.fr.data.Semantic;
import com.fr.data.TableData;

public class ADHOCSQLTableData extends ADHOCTableData
{
  public static int TYPE = 1;
  private String dbConnectionName = null;
  private String query = null;

  public ADHOCSQLTableData()
  {
  }

  public ADHOCSQLTableData(String paramString1, String paramString2, String paramString3, Semantic paramSemantic)
  {
    this.dbConnectionName = paramString1;
    this.query = paramString2;
    setDescription(paramString3);
    setSemantic(paramSemantic);
  }

  protected TableData createTableData()
  {
    if (this.dt == null)
    {
      DatasourceManager localDatasourceManager = FRContext.getDatasourceManager();
      if ((StringUtils.isNotEmpty(this.query)) && (StringUtils.isNotEmpty(this.dbConnectionName)))
        this.dt = new DBTableData((DatabaseConnection)localDatasourceManager.getConnection(this.dbConnectionName), this.query);
    }
    return this.dt;
  }

  public String getDbConnectionName()
  {
    return this.dbConnectionName;
  }

  public void setDbConnectionName(String paramString)
  {
    this.dbConnectionName = paramString;
  }

  public String getQuery()
  {
    return this.query;
  }

  public void setQuery(String paramString)
  {
    this.query = paramString;
  }

  public void readXML(XMLableReader paramXMLableReader)
  {
    super.readXML(paramXMLableReader);
    if (paramXMLableReader.isChildNode())
    {
      String str = paramXMLableReader.getTagName();
      if ("DBConnectionName".equals(str))
        setDbConnectionName(paramXMLableReader.getElementValue());
      else if ("Query".equals(str))
        setQuery(paramXMLableReader.getElementValue());
    }
  }

  public void writeXML(XMLPrintWriter paramXMLPrintWriter)
  {
    super.writeXML(paramXMLPrintWriter);
    if (StringUtils.isNotEmpty(this.dbConnectionName))
      paramXMLPrintWriter.startTAG("DBConnectionName").textNode(this.dbConnectionName).end();
    if (StringUtils.isNotEmpty(this.query))
      paramXMLPrintWriter.startTAG("Query").textNode(this.query).end();
  }

  public void parseJSON(JSONObject paramJSONObject)
    throws Exception
  {
    super.parseJSON(paramJSONObject);
    if (paramJSONObject.has("dbConnectionName"))
      setDbConnectionName(paramJSONObject.getString("dbConnectionName"));
    if (paramJSONObject.has("query"))
      setQuery(paramJSONObject.getString("query"));
  }

  public JSONObject createJSON()
    throws Exception
  {
    JSONObject localJSONObject = super.createJSON();
    localJSONObject.put("type", TYPE);
    if (StringUtils.isNotEmpty(this.dbConnectionName))
      localJSONObject.put("dbConnectionName", getDbConnectionName());
    if (StringUtils.isNotEmpty(this.query))
      localJSONObject.put("query", getQuery());
    return localJSONObject;
  }

  public boolean equals(Object paramObject)
  {
    return ((paramObject instanceof ADHOCSQLTableData) && (ComparatorUtils.equals(this.dbConnectionName, ((ADHOCSQLTableData)paramObject).dbConnectionName)) && (ComparatorUtils.equals(this.query, ((ADHOCSQLTableData)paramObject).query)) && (ComparatorUtils.equals(getDescription(), ((ADHOCSQLTableData)paramObject).getDescription())) && (ComparatorUtils.equals(getSemantic(), ((ADHOCSQLTableData)paramObject).getSemantic())));
  }

  public Object clone()
    throws CloneNotSupportedException
  {
    ADHOCSQLTableData localADHOCSQLTableData = (ADHOCSQLTableData)super.clone();
    localADHOCSQLTableData.setDbConnectionName(this.dbConnectionName);
    localADHOCSQLTableData.setQuery(this.query);
    return localADHOCSQLTableData;
  }
}